import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import '../../../shared/utils/responsive_utils.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../core/providers/auth_provider.dart';
import '../../../core/providers/error_handler_provider.dart';

class UnauthorizedPage extends ConsumerWidget {
  const UnauthorizedPage({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return Scaffold(
      body: SafeArea(
        child: Center(
          child: Padding(
            padding: EdgeInsets.all(
              ResponsiveUtils.getResponsiveSpacing(context, 24.0),
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                // 错误图标
                Container(
                  width: ResponsiveUtils.getResponsiveIconSize(context, 120),
                  height: ResponsiveUtils.getResponsiveIconSize(context, 120),
                  decoration: BoxDecoration(
                    color: Theme.of(context).colorScheme.errorContainer,
                    borderRadius: BorderRadius.circular(
                      ResponsiveUtils.getResponsiveRadius(context, 60),
                    ),
                  ),
                  child: Icon(
                    Icons.lock_outline,
                    size: ResponsiveUtils.getResponsiveIconSize(context, 60),
                    color: Theme.of(context).colorScheme.onErrorContainer,
                  ),
                ),
                SizedBox(
                  height: ResponsiveUtils.getResponsiveSpacing(context, 32),
                ),

                // 错误标题
                Text(
                  '登录已过期',
                  style: Theme.of(context).textTheme.headlineMedium?.copyWith(
                    fontWeight: FontWeight.bold,
                    color: Theme.of(context).colorScheme.error,
                  ),
                ),
                SizedBox(
                  height: ResponsiveUtils.getResponsiveSpacing(context, 16),
                ),

                // 错误描述
                Text(
                  '您的登录状态已过期，请重新登录以继续使用',
                  textAlign: TextAlign.center,
                  style: Theme.of(context).textTheme.bodyLarge?.copyWith(
                    color: Theme.of(
                      context,
                    ).colorScheme.onSurface.withOpacity(0.7),
                  ),
                ),
                SizedBox(
                  height: ResponsiveUtils.getResponsiveSpacing(context, 40),
                ),

                // 重新登录按钮
                FilledButton(
                  onPressed: () => _handleReLogin(context, ref),
                  style: FilledButton.styleFrom(
                    padding: EdgeInsets.symmetric(
                      horizontal: ResponsiveUtils.getResponsiveSpacing(
                        context,
                        32,
                      ),
                      vertical: ResponsiveUtils.getResponsiveSpacing(
                        context,
                        16,
                      ),
                    ),
                  ),
                  child: Text(
                    '重新登录',
                    style: TextStyle(
                      fontSize: ResponsiveUtils.getResponsiveFontSize(
                        context,
                        16,
                      ),
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                ),
                SizedBox(
                  height: ResponsiveUtils.getResponsiveSpacing(context, 16),
                ),

                // 返回首页按钮
                OutlinedButton(
                  onPressed: () => context.go('/dashboard'),
                  style: OutlinedButton.styleFrom(
                    padding: EdgeInsets.symmetric(
                      horizontal: ResponsiveUtils.getResponsiveSpacing(
                        context,
                        32,
                      ),
                      vertical: ResponsiveUtils.getResponsiveSpacing(
                        context,
                        16,
                      ),
                    ),
                  ),
                  child: Text(
                    '返回首页',
                    style: TextStyle(
                      fontSize: ResponsiveUtils.getResponsiveFontSize(
                        context,
                        16,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  void _handleReLogin(BuildContext context, WidgetRef ref) async {
    try {
      // 清除用户登录状态
      await ref.read(authProvider.notifier).logout();

      // 清除错误状态
      ref.read(errorHandlerProvider.notifier).clearError();

      // 跳转到登录页面
      if (context.mounted) {
        context.go('/login');
      }
    } catch (e) {
      if (context.mounted) {
        ScaffoldMessenger.of(
          context,
        ).showSnackBar(SnackBar(content: Text('重新登录失败：$e')));
      }
    }
  }
}
